package name.wilbur.course.db.bigone.util;

import java.sql.Timestamp;
import java.util.Date;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import org.apache.commons.lang3.time.DateUtils;;

public class MysqlTimestampWrapper {
	/* time format got from 
	 * 	http://www.jarvana.com/jarvana/view/org/apache/struts/struts2-dojo-plugin/2.1.8/struts2-dojo-plugin-2.1.8-sources.jar!/org/apache/struts2/dojo/components/DateTimePicker.java?format=ok 
	 */
	private static final String RFC3339_FORMAT = "yyyy-MM-dd'T'HH:mm:ss";
//	private static final String RFC3339_PATTERN = "{0,date," + RFC3339_FORMAT + "}";
	private static final DateFormat timeFormat = new SimpleDateFormat(RFC3339_FORMAT);
	private static final DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
	
	protected MysqlTimestampWrapper() {
	}
	
	public static Timestamp getCurrentTimestamp() {
		Date now = DateUtils.truncate(new Date(), Calendar.SECOND);
		return new Timestamp(now.getTime());
	}
	
	public static Date getSimpleTime(String date) {
		if (date == null || date.equals(""))
			return null;
		
		Date now = null;
		try {
			now = (Date) timeFormat.parse(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return getSimpleTime(now);
	}
	
	public static Date getSimpleTime(Date date) {
		if (date == null)
			return null;
		return DateUtils.truncate(date, Calendar.SECOND);
	}
	
	public static Date getSimpleDate(String date) {
		if (date == null || date.equals(""))
			return null;
		
		Date now = null;
		try {
			now = (Date) dateFormat.parse(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return getSimpleTime(now);
	}
	
	public static Date getSimpleDate(Date date) {
		if (date == null)
			return null;
		return DateUtils.truncate(date, Calendar.DAY_OF_MONTH);
	}
	
	public static java.sql.Date getSqlTime(Date date) {
		if (date == null)
			return null;
		return new java.sql.Date(getSimpleTime(date).getTime());
	}
	
	public static java.sql.Date getSqlTime(String date) {
		return getSqlDate(getSimpleTime(date));
	}
	
	public static java.sql.Date getSqlDate(Date date) {
		if (date == null)
			return null;
		return new java.sql.Date(getSimpleDate(date).getTime());
	}
	
	public static java.sql.Date getSqlDate(String date) {
		return getSqlDate(getSimpleDate(date));
	}
	
	public static Timestamp getSimpleTimestamp(Date date) {
		return new Timestamp(getSimpleDate(date).getTime());
	}
	
	public static Timestamp getSimpleTimestamp(String date) {
		return new Timestamp(getSimpleDate(date).getTime());
	}
}
